package com.zhuoyue.biz.core.command.cmd.common.fc8.base;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.constant.DataBase;
import com.zhuoyue.common.utils.CmdUtils;

/**
 * @ClassName: BaseCmd0Fc8
 * @Description: 计量控制
 * @author wjming
 * @Date 2021-07-14 16:05
 */
public class BaseCmd0Fc8 {
    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (controlReq.getControlType()) {
            case ControlType.TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, open());
                break;
            case ControlType.TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, close());
                break;

            case ControlType.TIMING_TURN_ON_REQUEST:
                int delayTime1 = controlReq.getActions().get("delayTime") == null ? 0 : (int) controlReq.getActions().get("delayTime");
                cmd = getControlCmd(deviceAndGatewayDO, openConfirmation(CmdUtils.getHexString(delayTime1)));
                break;
            case ControlType.TIMING_TURN_OFF_REQUEST:
                int delayTime2 = controlReq.getActions().get("delayTime") == null ? 0 : (int) controlReq.getActions().get("delayTime");
                cmd = getControlCmd(deviceAndGatewayDO, closeConfirmation(CmdUtils.getHexString(delayTime2)));
                break;
            default:
                break;
        }
        return cmd;
    }

    /**
     * 开
     *
     * @return
     */
    public static String open() {
        return "00020000" + "01";
    }

    /**
     * 关
     *
     * @return
     */
    public static String close() {
        return "00020000" + "02";
    }


    /**
     * 延时开
     *
     * @return
     */
    public static String openConfirmation(String time) {
        return "00060000" + "01" + time;
    }

    /**
     * 延时关
     *
     * @return
     */
    public static String closeConfirmation(String time) {
        return "00060000" + "02" + time;
    }


    /**
     * 拼接数据帧
     *
     * @param deviceAndGatewayDO
     * @param cmd10To12
     * @return
     */
    public static String getControlCmd(DeviceAndGatewayDO deviceAndGatewayDO, String cmd10To12) {

        String cmd9 = CmdUtils.getDateLength(cmd10To12);

        String cmd3To12 = DataBase.WRITE +
                DataBase.ONE +
                deviceAndGatewayDO.getGatewayMac() +
                deviceAndGatewayDO.getZigbeeMac() +
                deviceAndGatewayDO.getOdIndex() +
                DataBase.VARIABLE_INDEX +
                cmd9 +
                cmd10To12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = DataBase.DATA_HEAD +
                cmd2 +
                cmd3To12 +
                cmd13 +
                DataBase.DATA_END;

        return fullCmd;
    }

}
